﻿
// First: Create config
ko.validation.configure({
    registerExtenders: true,
    messagesOnModified: true,
    insertMessages: true,
    parseInputAttributes: true,
    messageTemplate: null,
    decorateElement: true,
    errorClass: "error",
    errorMessageClass: "error-desc"
});

var previewModel = new function () {
    
    this.Save = function()
    {
        var prodId = $('#productId').val();
        UpdateProductPreview(prodId, "save");
    };
    this.SaveTransaction = function()
    {
        var prodId = $('#productId').val();
        UpdateProductPreview(prodId, "savetransaction");
    };
    this.ClosePopup = function(){
        closePopup();
    };

    function UpdateProductPreview(id, action)
    {
        // Json post
        var urlPost = "/Management/ProductList/UpdatePreview";
        var dataPost = { "id": id, "action": action};

        // Json Get function to retrive data from server
        $('.waiting').remove();
        $('#apps').waiting();
        $('.waiting').show();
        $('#progress').css("display", "block");

        // Ajax request
        $.ajax({
            type: "POST",
            url: urlPost,
            data: dataPost,
            dataType: "json",
            success: function (model, textStatus, jqXHR) {
                // Stop progress bar
                $('.waiting').hide();
                $('#progress').css("display", "none");

                if (model != null) {
                    closePopup();
                }
                else {
                    showPopupError();
                }
            },
            error: function (xhr, status, error) {
                showPopupError();
            }
        });
        // End Ajax Request
    }
}

ko.applyBindings(previewModel);